﻿using Max.PH4.Shoppgin.Admin.DataAccess;
using Max.PH4.Shoppgin.Admin.Domain;
using Max.PH4.Shoppgin.Admin.IRespository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;

namespace Max.PH4.Shoppgin.Admin.Respository
{
    /*
     * 仓储实现类：通过数据访问层的方法，实现对数据的CRUD操作
     * 系统用户的仓储实现类
     * 1、它需要继承自己的仓储接口，即：ISystemUserRespository
     * 2、继承了接口，那么就需要实现这个接口的方法
     * **/
    public class SystemUserRespository : ISystemUserRespository
    {
        MyDbContext _context;


        public SystemUserRespository(MyDbContext context)
        {
            _context = context;
        }

        /*
         * SaveChanges()方法表示，将Remove的操作后的数据同步到数据库
         * RemoveRange()方法：删除范围，表示可以批量删除
         * **/

        public int Delete(SystemUserEntity entity)
        {
            _context.SystemUsers.Remove(entity);
            //_context.SystemUsers.RemoveRange(entity);
            int result = _context.SaveChanges();
            return result;
        }

        public int Delete(object id)
        {
            var m = _context.SystemUsers.Find(id);
            if (m != null)
            {
                _context.SystemUsers.Remove(m);
                int result = _context.SaveChanges();
                return result;
            }
            return -1;
        }

        public int Insert(SystemUserEntity entity)
        {
            _context.SystemUsers.Add(entity);

            int result = _context.SaveChanges();
            return result;
        }

        public IQueryable<SystemUserEntity> Query()
        {
            //var list = _context.SystemUsers.FromSqlRaw($"pr_get_system_user '{value}'"); //.ExexcuteSqlCommand
            //return list;
            return _context.SystemUsers.AsQueryable();
        }

        public int Update(SystemUserEntity entity)
        {
            _context.SystemUsers.Update(entity);

            int result = _context.SaveChanges();
            return result;
        }
    }

    #region 扩展方法

    //这个persion类是别人写的，自己不能修改
    public class Person
    {
        public void FirstHand() { }
        public void SecondHand() { }
        public void FirstLeg() { }
        public void SecondLeg() { }
    }

    //这个是自己写的，对person类进行扩展
    public static  class MyPersonExt
    {
        public static void ThirdHand(this Person p) { }
    }

    public class 梁才根
    {
        public void 跑()
        {
            Person p = new Person();
            p.FirstHand();
            p.SecondHand();
            p.FirstLeg();
            p.SecondLeg();
            p.ThirdHand();
        }
    }

    #endregion
}
